<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="关键字" prop="searchValue">
        <el-input v-model="queryParams.searchValue" placeholder="请输入关键字" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="入库状态" prop="storageStatus">
        <el-select v-model="queryParams.storageStatus" placeholder="请选择入库状态" clearable @change="handleQuery">
          <el-option v-for="dict in dict.type.storage_status" :key="dict.label" :label="dict.label" :value="dict.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="归属渠道" prop="channelId">
        <div style="max-width: 500px">
          <treeselect v-model="queryParams.channelId" :options="channelOptions" :show-count="true" placeholder="请选择渠道" noResultsText="暂无数据"
            @input="handleQuery" />
        </div>
      </el-form-item>
      <el-form-item label="归属单位" prop="companyId">
        <SelectCompany
          :defaultValue="queryParams.companyId"
          :companyType="0"
          :channelId="queryParams.channelId"
          @change="(e) => upDataSearch('companyId', e)"
        />
      </el-form-item>
      <el-form-item label="归属部门" prop="deptName">
        <SelectDept
          :defaultValue="queryParams.deptName"
          valueKey="deptName"
          :comId="queryParams.companyId"
          @change="(e) => upDataSearch('deptName', e)"
        />
      </el-form-item>
      <el-form-item label="激活状态" prop="activationStatus">
        <el-select v-model="queryParams.activationStatus" placeholder="请选择激活状态" clearable @change="handleQuery">
          <el-option v-for="dict in dict.type.activation_status" :key="dict.label" :label="dict.label"
            :value="dict.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="启用状态" prop="enabledStatus">
        <el-select v-model="queryParams.enabledStatus" placeholder="请选择当前启用状态" clearable @change="handleQuery">
          <el-option v-for="dict in dict.type.enabled_status" :key="dict.label" :label="dict.label" :value="dict.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="登录状态" prop="appLoginStatus">
        <el-select v-model="queryParams.appLoginStatus" placeholder="请选择登录状态" clearable @change="handleQuery">
          <el-option v-for="dict in dict.type.login_status" :key="dict.label" :label="dict.label" :value="dict.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="通话状态" prop="isVideoCall">
        <el-select v-model="queryParams.isVideoCall" placeholder="请选择通话状态" clearable @change="handleQuery">
          <el-option v-for="dict in dict.type.call_status" :key="dict.label" :label="dict.label" :value="dict.label">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="equipmentList" @selection-change="handleSelectionChange">
      <el-table-column label="设备序列号" align="center" prop="serialNo" width="180" />
      <el-table-column label="设备终端号" align="center" prop="terminalNo" width="100" />
      <el-table-column label="设备型号" align="center" prop="modelNo" width="100" />
      <el-table-column label="有线MAC地址" align="center" prop="wiredMacaddr" width="140" />
      <el-table-column label="无线MAC地址" align="center" prop="wirelessMacaddr" width="140" />
      <el-table-column
        label="初始化时间"
        align="center"
        prop="initTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.initTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入库状态" align="center" prop="storageStatus" width="100" />
      <el-table-column
        label="入库时间"
        align="center"
        prop="storageTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.storageTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否已激活" align="center" prop="activationStatus" width="100" />
      <el-table-column label="当前固件版本" align="center" prop="firmwareVersion" width="200" />
      <el-table-column label="当前桌面版本" align="center" prop="desktopVersion" width="120" />
      <el-table-column label="当前APP版本" align="center" prop="appVersion" width="120" />
      <el-table-column
        label="所属事业部"
        align="center"
        prop="platformName"
        width="250"
      />
      <el-table-column label="所属渠道" align="center" prop="channelName" min-width="250" />
      <el-table-column label="归属单位" align="center" prop="companyName" min-width="250" />
      <el-table-column label="归属部门" align="center" prop="deptName" min-width="200" />
      <el-table-column label="当前启用状态" align="center" prop="enabledStatus" width="150" />
      <el-table-column label="APP登录状态" align="center" prop="appLoginStatus" width="120">
        <template slot-scope="scope">
          <p v-if="scope.row.appLoginStatus === '在线'">
            {{ scope.row.appLoginStatus }}{{ scope.row.appLoginUser }}
          </p>
          <p v-else>{{ scope.row.appLoginStatus }}</p>
        </template>
      </el-table-column>
      <el-table-column label="通话状态" align="center" prop="isVideoCall" width="120" />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.params.pageNum"
      :limit.sync="queryParams.params.pageSize" @pagination="getList" />

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :show-message="false" label-width="80px">
        <el-form-item label="设备序列号" prop="serialNo">
          <el-input v-model="form.serialNo" placeholder="请输入设备序列号" />
        </el-form-item>
        <el-form-item label="设备终端号" prop="terminalNo">
          <el-input v-model="form.terminalNo" placeholder="请输入设备终端号" />
        </el-form-item>
        <el-form-item label="型号" prop="modelNo">
          <el-input v-model="form.modelNo" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="初始化时间" prop="initTime">
          <el-date-picker clearable v-model="form.initTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择初始化时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入库时间" prop="storageTime">
          <el-date-picker clearable v-model="form.storageTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择入库时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="所属渠道(来源于渠道表id)" prop="channelId">
          <el-input v-model="form.channelId" placeholder="请输入所属渠道(来源于渠道表id)" />
        </el-form-item>
        <el-form-item label="归属单位(来源于单位表;id)" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入归属单位(来源于单位表;id)" />
        </el-form-item>
        <el-form-item label="是否已激活" prop="isActivated">
          <el-input v-model="form.isActivated" placeholder="请输入是否已激活" />
        </el-form-item>
        <el-form-item label="分配渠道时间" prop="channelTime">
          <el-date-picker clearable v-model="form.channelTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择分配渠道时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备激活时间" prop="activationTime">
          <el-date-picker clearable v-model="form.activationTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择设备激活时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备到期时间" prop="expirationTime">
          <el-date-picker clearable v-model="form.expirationTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择设备到期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备续费周期" prop="renewalCycle">
          <el-input v-model="form.renewalCycle" placeholder="请输入设备续费周期" />
        </el-form-item>
        <el-form-item label="续费提醒" prop="renewalReminder">
          <el-input v-model="form.renewalReminder" placeholder="请输入续费提醒" />
        </el-form-item>
        <el-form-item label="设备当前桌面端版本" prop="desktopVersion">
          <el-input v-model="form.desktopVersion" placeholder="请输入设备当前桌面端版本" />
        </el-form-item>
        <el-form-item label="设备当前APP版本" prop="appVersion">
          <el-input v-model="form.appVersion" placeholder="请输入设备当前APP版本" />
        </el-form-item>
        <el-form-item label="是否视频通话中" prop="isVideoCall">
          <el-input v-model="form.isVideoCall" placeholder="请输入是否视频通话中" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEquipment,
  getEquipment,
  delEquipment,
  addEquipment,
  updateEquipment,
} from "@/api/equipment/equipment";
import { treeselect } from "@/api/channel/channel";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import SelectCompany from "@/components/SelectCompany";
import SelectDept from "@/components/SelectDept";

export default {
  name: "Statistics",
  dicts: [
    "storage_status",
    "sys_dept_saas_dept_id",
    "activation_status",
    "enabled_status",
    "version_set",
    "call_status",
    "login_status",
  ],
  components: { Treeselect, SelectCompany, SelectDept },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理表格数据
      equipmentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 渠道树选项
      channelOptions: [],
      // 查询参数
      queryParams: {
        params: { pageNum: 1, pageSize: 10 },
        searchType: 1,
        searchValue: null,
        channelId: null,
        companyId: null,
        storageStatus: null,
        deptName: null,
        activationStatus: null,
        enabledStatus: null,
        desktopVersion: null,
        appVersion: null,
        appLoginStatus: null,
        isVideoCall: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getTreeselect();
    this.getList();
  },
  activated() {
    this.getTreeselect();
    this.getList();
  },
  methods: {
    /** 查询设备管理列表 */
    getList() {
      this.loading = true;
      listEquipment(this.queryParams).then((response) => {
        this.equipmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询渠道下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.channelOptions = response.data;
        this.channelOptions.push({ id: 0, label: "未分配" });
      });
    },
    upDataSearch(key, value) {
      this.queryParams[key] = value;
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serialNo: null,
        terminalNo: null,
        modelNo: null,
        storageStatus: "0",
        initTime: null,
        storageTime: null,
        remark: null,
        channelId: null,
        companyId: null,
        isActivated: null,
        channelTime: null,
        activationTime: null,
        activationStatus: "0",
        expirationTime: null,
        renewalCycle: null,
        renewalReminder: null,
        enabledStatus: "0",
        desktopVersion: null,
        appVersion: null,
        appLoginStatus: "0",
        isVideoCall: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.params.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.searchValue = null;
      this.queryParams.channelId = null;
      this.queryParams.companyId = null;
      this.queryParams.storageStatus = null;
      this.queryParams.deptName = null;
      this.queryParams.activationStatus = null;
      this.queryParams.enabledStatus = null;
      this.queryParams.desktopVersion = null;
      this.queryParams.appVersion = null;
      this.queryParams.appLoginStatus = null;
      this.queryParams.isVideoCall = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getEquipment(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateEquipment(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEquipment(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除设备管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delEquipment(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>
